package com.yunfeisoft.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.applet.base.BaseServiceImpl;
import com.applet.sql.separation.DataSourceChange;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.SmsHomeNewProductDao;
import com.yunfeisoft.business.model.PmsProduct;
import com.yunfeisoft.business.model.SmsHomeNewProduct;
import com.yunfeisoft.business.service.inter.SmsHomeNewProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SmsHomeNewProductServiceImpl
 * Description: 新鲜好物表service实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Service("smsHomeNewProductService")
public class SmsHomeNewProductServiceImpl extends BaseServiceImpl<SmsHomeNewProduct, String, SmsHomeNewProductDao> implements SmsHomeNewProductService {

    @Autowired
    private SmsHomeNewProductDao smsHomeNewProductDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<SmsHomeNewProduct> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    /**
     * 添加新鲜好物商品
     * @param multipleSelection
     * @return
     */
    @Transactional
    @Override
    public Map<String, Object> saveHomeNewProduct(String multipleSelection) {
        Map<String, Object> result = new HashMap<>();
        // 将参数转为品牌列表
        List<PmsProduct> pmsProductList = JSON.parseArray(multipleSelection, PmsProduct.class);
        for (PmsProduct pmsProduct : pmsProductList) {
            // 查询品牌是否已被添加
            List<SmsHomeNewProduct> smsHomeNewProductList = smsHomeNewProductDao.queryByProductId(pmsProduct.getId());
            if (!ObjectUtils.isEmpty(smsHomeNewProductList)) {
                result.put("flag", false);
                result.put("message", "商品已被添加!");
                return result;
            }
        }
        // 新增
        pmsProductList.stream().forEach(pmsProduct -> {
            SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
            smsHomeNewProduct.setProductId(pmsProduct.getId());
            smsHomeNewProduct.setProductName(pmsProduct.getName());
            // 排序默认为0
            smsHomeNewProduct.setSort(0);
            // 默认不推荐0
            smsHomeNewProduct.setRecommendStatus(0);
            save(smsHomeNewProduct);
        });
        result.put("flag", true);
        result.put("message", "添加商品成功!");
        return result;
    }

    /**
     * 分页查询新鲜好物
     * @param params
     * @return
     */
    @Override
    public Page<SmsHomeNewProduct> queryHomeNewProductPage(Map<String, Object> params) {
        return getDao().queryHomeNewProductPage(params);
    }

    /**
     * 查询首页新鲜好物列表
     * @param limit
     * @return
     */
    @Override
    public List<SmsHomeNewProduct> queryHomeNewProductIndex(int limit) {
        return getDao().queryHomeNewProductIndex(limit);
    }
}